#include <stdio.h>
#include <stdlib.h>

#define MAX_N 100
#define MAX_M 100

typedef struct {
    int x;
    int y;
} Point;

int n, m;
int g[MAX_N][MAX_M];
int vis[MAX_N][MAX_M];
Point d[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
Point q[MAX_N * MAX_M];
int front = 0, rear = 0;
//广度优先搜索，从起点(x,y)出发，到终点(x2,y2)的最短路径长度
int bfs(int x, int y, int x2, int y2) {
    q[rear].x = x;//将x,y加入到队列q中
    q[rear].y = y;
    rear++;
    vis[x][y] = 1;//标记为已访问

    while (front < rear) {//进入循环，直到队列不为空为止
        Point now = q[front];//取出点now
        front++;

        int nx = now.x;//将值赋给nx和ny
        int ny = now.y;

        if (nx == x2 - 1 && ny == y2 - 1) {//判断是否达到终点（x2,y2)，是就跳出循环结束搜索
            break;
        }

        for (int i = 0; i < 4; i++) {//遍历四个相邻的点，计算它们的坐标
            int dx = nx + d[i].x;
            int dy = ny + d[i].y;

            if (dx >= 0 && dx < n && dy >= 0 && dy < m && vis[dx][dy] == 0 && g[dx][dy] == 1) {
              //判断这些点是否在举证范围内，且未被访问，且g[dx][dy] == 1：可达
                q[rear].x = dx;//如果满足条件，则加入队列q中，并更新该点的访问状态和路径长度
                q[rear].y = dy;
                rear++;
                vis[dx][dy] = vis[nx][ny] + 1;
            }
        }
    }

    return vis[x2 - 1][y2 - 1];//返回(x2,y2)的最短路径长度
}

int main() {
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d", &g[i][j]);
        }
    }

    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    int result = bfs(x1 - 1, y1 - 1, x2, y2) - 1;
    printf("%d\n", result);

    return 0;
}
